<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="关联配置ID（关联member_map_config.id）" prop="configId">
        <el-input v-model="formData.configId" placeholder="请输入关联配置ID（关联member_map_config.id）" />
      </el-form-item>
      <el-form-item label="多边形名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入多边形名称" />
      </el-form-item>
      <el-form-item label="描边颜色（16进制色值）" prop="strokeColor">
        <el-input v-model="formData.strokeColor" placeholder="请输入描边颜色（16进制色值）" />
      </el-form-item>
      <el-form-item label="描边宽度（单位：像素，范围：1~20）" prop="strokeWeight">
        <el-input v-model="formData.strokeWeight" placeholder="请输入描边宽度（单位：像素，范围：1~20）" />
      </el-form-item>
      <el-form-item label="填充颜色（16进制色值）" prop="fillColor">
        <el-input v-model="formData.fillColor" placeholder="请输入填充颜色（16进制色值）" />
      </el-form-item>
      <el-form-item label="填充透明度（范围：0.00~1.00）" prop="fillOpacity">
        <el-input v-model="formData.fillOpacity" placeholder="请输入填充透明度（范围：0.00~1.00）" />
      </el-form-item>
      <el-form-item label="层级（值越大越靠上，范围：1~100）" prop="zIndex">
        <el-input v-model="formData.zIndex" placeholder="请输入层级（值越大越靠上，范围：1~100）" />
      </el-form-item>
      <el-form-item label="排序（升序排列）" prop="sortOrder">
        <el-input v-model="formData.sortOrder" placeholder="请输入排序（升序排列）" />
      </el-form-item>
      <el-form-item label="多边形面积（单位：平方米，自动计算）" prop="area">
        <el-input v-model="formData.area" placeholder="请输入多边形面积（单位：平方米，自动计算）" />
      </el-form-item>
      <el-form-item label="顶点数量（自动计算）" prop="pointCount">
        <el-input v-model="formData.pointCount" placeholder="请输入顶点数量（自动计算）" />
      </el-form-item>
      <el-form-item label="创建人ID" prop="createdBy">
        <el-input v-model="formData.createdBy" placeholder="请输入创建人ID" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker
          v-model="formData.createdAt"
          type="date"
          value-format="x"
          placeholder="选择创建时间"
        />
      </el-form-item>
      <el-form-item label="更新人ID" prop="updatedBy">
        <el-input v-model="formData.updatedBy" placeholder="请输入更新人ID" />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker
          v-model="formData.updatedAt"
          type="date"
          value-format="x"
          placeholder="选择更新时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MapPolygonApi, MapPolygonVO } from '@/api/member/mappolygon'

/** 地图多边形信息 表单 */
defineOptions({ name: 'MapPolygonForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  configId: undefined,
  name: undefined,
  strokeColor: undefined,
  strokeWeight: undefined,
  fillColor: undefined,
  fillOpacity: undefined,
  zIndex: undefined,
  sortOrder: undefined,
  area: undefined,
  pointCount: undefined,
  createdBy: undefined,
  createdAt: undefined,
  updatedBy: undefined,
  updatedAt: undefined
})
const formRules = reactive({
  configId: [{ required: true, message: '关联配置ID（关联member_map_config.id）不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '多边形名称不能为空', trigger: 'blur' }],
  strokeColor: [{ required: true, message: '描边颜色（16进制色值）不能为空', trigger: 'blur' }],
  strokeWeight: [{ required: true, message: '描边宽度（单位：像素，范围：1~20）不能为空', trigger: 'blur' }],
  fillColor: [{ required: true, message: '填充颜色（16进制色值）不能为空', trigger: 'blur' }],
  fillOpacity: [{ required: true, message: '填充透明度（范围：0.00~1.00）不能为空', trigger: 'blur' }],
  zIndex: [{ required: true, message: '层级（值越大越靠上，范围：1~100）不能为空', trigger: 'blur' }],
  sortOrder: [{ required: true, message: '排序（升序排列）不能为空', trigger: 'blur' }],
  createdBy: [{ required: true, message: '创建人ID不能为空', trigger: 'blur' }],
  createdAt: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
  updatedAt: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MapPolygonApi.getMapPolygon(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MapPolygonVO
    if (formType.value === 'create') {
      await MapPolygonApi.createMapPolygon(data)
      message.success(t('common.createSuccess'))
    } else {
      await MapPolygonApi.updateMapPolygon(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    configId: undefined,
    name: undefined,
    strokeColor: undefined,
    strokeWeight: undefined,
    fillColor: undefined,
    fillOpacity: undefined,
    zIndex: undefined,
    sortOrder: undefined,
    area: undefined,
    pointCount: undefined,
    createdBy: undefined,
    createdAt: undefined,
    updatedBy: undefined,
    updatedAt: undefined
  }
  formRef.value?.resetFields()
}
</script>